<?php

namespace app\api\services\user;

use app\api\constant\JoinStatusConst;
use app\api\model\JoinOnboarding;
use app\api\model\JoinSkill;
use app\api\services\BaseServices;
use Exception;

/**
 * 接单员入驻服务层
 */
class JoinOnboardingServices extends BaseServices
{
    /**
     * 骑手入驻
     * @param array $params
     * @return void
     * @throws \think\Exception
     * @throws Exception
     */
    public function enter(array $params)
    {
        $joinOnboarding = JoinOnboarding::getByUserId($params['user_id']);
        if ($joinOnboarding && $joinOnboarding['status'] != JoinStatusConst::SUCCESS) {
            throw new Exception('您已经申请入驻，请耐心等待审核结果');
        }
        if (!empty($params['skill_ids'])) {
            if (!is_array($params['skill_ids'])) {
                $params['skill_ids'] = explode(',', $params['skill_ids']);
            }
        }
        JoinOnboarding::modelCreate($params);
    }

    /**
     *
     * @param $userid
     * @return array
     */
    public function getJoinMerchant($userid): array
    {
        return JoinOnboarding::getByUserId($userid)->toArray() ?? [];
    }

    /**
     * 装饰用户选择的技能
     * @param array $joinMerchant
     * @return void
     */
    public function decorationSkillChecked(array &$joinMerchant)
    {
        if (!empty($joinMerchant['skill_ids'])) {
            $skill_ids = $joinMerchant['skill_ids'];
            $ids = JoinSkill::select();

            $joinMerchant['skill'] = [];
            foreach ($ids as $v) {
                $skillItem = [
                    'id' => $v['id'],
                    'name' => $v['name'],
                    'checked' => in_array($v['id'], $skill_ids)
                ];
                $joinMerchant['skill'][] = $skillItem;
            }
        }
        unset($joinMerchant);
    }
}